//
//  BSCalendarCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/25.
//

import Foundation
import JTAppleCalendar

class BSCalendarCell: JTACDayCell {
    var selectedRoundColor: UIColor = UIColor.hex("#306EC8")
    var selectableDateColor: UIColor = .darkGray

    private let preDateSelectable: Bool = true
    
    private var style: BSCalendarEnum = .Default
    
    func configViews(withStyle _style: BSCalendarEnum) {
        removeAllSubViews()
        
        style = _style

        switch _style {
        case .Default, .MonthStatus: configWorkStatus(); break
        case .Attendance, .TaskRate: configAttendance(); break
        case .WorkStatus:
            configWorkStatus()
            dayLabel.layer.borderColor = UIColor.hex("#306EC8").cgColor
            dayLabel.layer.borderWidth = 1
            dayLabel.layer.masksToBounds = true
            break
        case .TaskRateDetail: configTaskRateDetail(); break
        }
    }
    
    func handleCellSelection(cellState: CellState, date: Date, selectedDate: Date, rwwcxqModal: RwwcxqModel) {
        
        selectableDateColor = rwwcxqModal.wcFlag == 1 ? UIColor.hex("#B2B5B9") : UIColor.hex("#FF0000")
        
        if cellState.dateBelongsTo != .thisMonth {
            isUserInteractionEnabled = false
            dayLabel.text = ""
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            stableBackView.isHidden = true
            line.backgroundColor = .clear
        } else if date.isSmaller(to: Date()) && !preDateSelectable {
            isUserInteractionEnabled = false
            dayLabel.text = "-"
            dayLabel.textColor = UIColor.white
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            stableBackView.isHidden = true
            line.backgroundColor = .clear
        } else {
            isUserInteractionEnabled = true
            stableBackView.isHidden = false
            dayLabel.text = cellState.text
            dayLabel.textColor = selectableDateColor
            line.backgroundColor = selectableDateColor
        }
        
        configueViewIntoBubbleView(cellState, date: date)
    }
    
    func handleCellSelection(cellState: CellState, date: Date, selectedDate: Date, yyModal: YywdModel?) {
        
        selectableDateColor = yyModal?.yyzt == "1" ? UIColor.hex("#306EC8") : UIColor.hex("#F17854")
        selectedRoundColor = yyModal?.yyzt == "1" ? UIColor.hex("#306EC8") : UIColor.hex("#F17854")
        
        if cellState.dateBelongsTo != .thisMonth {
            isUserInteractionEnabled = false
            dayLabel.text = ""
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            stableBackView.isHidden = true
        } else if date.isSmaller(to: Date()) && !preDateSelectable {
            isUserInteractionEnabled = false
            dayLabel.text = "-"
            dayLabel.textColor = UIColor.white
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            stableBackView.isHidden = true
        } else {
            isUserInteractionEnabled = true
            stableBackView.isHidden = false
            dayLabel.text = cellState.text
            dayLabel.layer.borderColor = selectedRoundColor.cgColor
            dayLabel.layer.borderWidth = 1
            dayLabel.layer.masksToBounds = true
            dayLabel.textColor = selectableDateColor
        }
        
        configueViewIntoBubbleView(cellState, date: date)
    }
    
    func handleCellSelection(cellState: CellState, date: Date, selectedDate: Date, ratesModal: WclByMonthDateModel) {
        selectableDateColor = UIColor.hex("#306EC8")
        //InDate, OutDate
        if cellState.dateBelongsTo != .thisMonth {
            detailLabel.text = ""
        } else if date.isSmaller(to: Date()) && !preDateSelectable {
            detailLabel.text = "-"
        } else {
            detailLabel.text = "\(ratesModal.zwcl ?? "0")%"
            detailLabel.textColor = .hex("#333333")
        }
        handleCellSelection(cellState: cellState, date: date, selectedDate: selectedDate)
    }
    
    func handleCellSelection(cellState: CellState, date: Date, selectedDate: Date, siModal: StaticsItemModel?) {
        
        //InDate, OutDate
        if cellState.dateBelongsTo != .thisMonth {
            detailLabel.text = ""
        } else if date.isSmaller(to: Date()) && !preDateSelectable {
            detailLabel.text = "-"
        } else {
            detailLabel.text = siModal?.statusDesc
            detailLabel.textColor = siModal?.status == 0 ? .hex("#939DB1") : .hex("#FF1E00")
        }
        handleCellSelection(cellState: cellState, date: date, selectedDate: selectedDate)
    }
    
    func handleCellSelectionDisabled(cellState: CellState, date: Date, selectedDate: Date) {
        //InDate, OutDate
        if cellState.dateBelongsTo != .thisMonth {
            dayLabel.text = ""
            detailLabel.text = ""
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            isUserInteractionEnabled = false
            stableBackView.isHidden = true
        } else if date.isSmaller(to: Date()) {
            dayLabel.text = cellState.text
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            dayLabel.textColor = UIColor.lightGray
            isUserInteractionEnabled = false
            stableBackView.isHidden = true
        } else {
            stableBackView.isHidden = false
            isUserInteractionEnabled = true
            dayLabel.text = cellState.text
            dayLabel.textColor = UIColor.hex("#F17854")
            if style != .WorkStatus {
                dayLabel.layer.borderColor = UIColor.clear.cgColor
                dayLabel.layer.borderWidth = 0
            }
            selectableDateColor = UIColor.hex("#306EC8")
            selectedRoundColor = UIColor.hex("#306EC8")
        }
        
        configueViewIntoBubbleView(cellState, date: date)
    }
    
    func handleCellSelection(cellState: CellState, date: Date, selectedDate: Date) {
        //InDate, OutDate
        if cellState.dateBelongsTo != .thisMonth {
            dayLabel.text = ""
            detailLabel.text = ""
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            isUserInteractionEnabled = false
            stableBackView.isHidden = true
            line.backgroundColor = .clear
        } else if date.isSmaller(to: Date()) && !preDateSelectable {
            dayLabel.text = "-"
            detailLabel.text = "-"
            dayLabel.layer.borderColor = UIColor.clear.cgColor
            dayLabel.layer.borderWidth = 0
            dayLabel.textColor = UIColor.white
            isUserInteractionEnabled = false
            stableBackView.isHidden = true
            line.backgroundColor = .clear
        } else {
            stableBackView.isHidden = false
            isUserInteractionEnabled = true
            dayLabel.text = cellState.text
            dayLabel.textColor = selectableDateColor
            if style != .WorkStatus {
                dayLabel.layer.borderColor = UIColor.clear.cgColor
                dayLabel.layer.borderWidth = 0
            }
        }
        
        configueViewIntoBubbleView(cellState, date: date)
    }
    
    func cellSelectionChanged(_ cellState: CellState, date: Date) {
        if cellState.isSelected == true {
            if selectedView.isHidden == true {
                configueViewIntoBubbleView(cellState, date: date)
            }
        } else {
            configueViewIntoBubbleView(cellState, date: date)
        }
    }
    
    fileprivate func configueViewIntoBubbleView(_ cellState: CellState, date: Date) {
        if cellState.isSelected && isUserInteractionEnabled {
            selectedView.isHidden = false
            if style != .MonthStatus {
                selectedView.layer.backgroundColor = selectedRoundColor.cgColor
                dayLabel.textColor = .white
            } else {
                selectedView.layer.borderColor = selectedRoundColor.cgColor
                selectedView.layer.borderWidth = 1
                dayLabel.textColor = selectableDateColor
            }
        } else {
            selectedView.isHidden = true
            if style != .MonthStatus {
                dayLabel.textColor = selectableDateColor
            } else {
                if date.isSmaller(to: Date()) {
                    dayLabel.textColor = .lightGray
                } else {
                    dayLabel.textColor = UIColor.hex("#F17854")
                }
            }
        }
    }
    
    func configWorkStatus() {
        addSubview(stableBackView)
        stableBackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        selectedView.layer.cornerRadius = 20
        addSubview(selectedView)
        selectedView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        dayLabel.layer.cornerRadius = 20
        addSubview(dayLabel)
        dayLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40)
        }
    }
        
    func configAttendance() {
        addSubview(stableBackView)
        stableBackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        selectedView.layer.cornerRadius = 15
        addSubview(selectedView)
        selectedView.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        addSubview(dayLabel)
        dayLabel.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        addSubview(detailLabel)
        detailLabel.snp.makeConstraints { make in
            make.top.equalTo(dayLabel.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
        }
    }
    
    func configTaskRateDetail() {
        addSubview(stableBackView)
        stableBackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        selectedView.layer.cornerRadius = 15
        addSubview(selectedView)
        selectedView.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        addSubview(dayLabel)
        dayLabel.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(30)
        }
        
        addSubview(line)
        line.snp.makeConstraints { make in
            make.top.equalTo(dayLabel.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
            make.width.equalTo(15)
            make.height.equalTo(3)
        }
    }
    
    lazy var stableBackView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var selectedView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var dayLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 15)
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 12)
        return label
    }()
    
    lazy var line: UIView = {
        let l = UIView()
        l.backgroundColor = .hex("#B2B5B9")
        l.layer.cornerRadius = 1.5
        l.layer.masksToBounds = true
        return l
    }()
}
